<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="dialogVisible=true">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type===1" size="mini" type="text" @click="add(row)">添加</el-button>
            <el-button size="mini" type="text" @click="redact(row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹出框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <el-form ref="faomData" :model="faomData" label-width="80px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="faomData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="faomData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="faomData.description" />
        </el-form-item>
        <!--  eslint-disable-next-line vue/no-parsing-error -->
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="faomData.enVisible" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item style=" margin-left:120px">
          <el-button type="primary" size="mini" @click="confirm">确 定</el-button>
          <el-button size="mini" @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermission, postPermission, delPermission, putPermission } from '@/api/permission'
import { transformData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      faomData: {
        name: '', // 权限点名字
        code: '', // 权限点标识
        description: '', // 权限点描述
        enVisible: 0, // 权限点开启状态,
        type: 0,
        pid: 0
      },
      title: '新增权限点',
      rules: {
        name: [
          { required: true, message: '请输入权限点名字', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限点标识', trigger: 'blur' }
        ] }
    }
  },
  async created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      const res = await getPermission()
      this.list = transformData(res.data, 0)
    },
    // 确定
    async confirm() {
      // 表单校验
      this.$refs.faomData.validate()
      if (this.title === '编辑权限点') {
        await putPermission(this.faomData)
        // 提示用户
        this.$message.success('编辑成功')
      } else if (this.title === '新增权限点') {
        // 发送请求
        await postPermission(this.faomData)
        // 提示用户
        this.$message.success('新增成功')
      }
      // 关闭弹窗
      this.dialogVisible = false
      // 清空表单
      this.$refs.faomData.resetFields()
      // 刷新页面
      this.getPermission()
    },
    cancel() {
      // 关闭弹窗
      this.dialogVisible = false
      // 清空表单
      this.$refs.faomData.resetFields()
    },
    // async add(row) {
    //   console.log(row)
    //   if (row.type === 1) {
    //     await postPermission(this.faomData)
    //     this.dialogVisible = true
    //   }
    // },
    // 删除
    async del(id) {
      await delPermission(id)
      // 弹出提示
      this.$message.success('删除成功')
      // 刷新数据
      this.getPermission()
    },
    // 编辑
    async redact(row) {
      this.title = '编辑权限点'
      this.dialogVisible = true
      this.faomData = row
    },
    add(row) {
      // 开启弹窗
      this.dialogVisible = true
      this.faomData.pid === row.id
      this.faomData.type = row.type
      // 清空表单
      this.$refs.faomData.resetFields()
    }
  }

}
</script>
<style>
.btn-add {
  margin: 10px;

}
</style>
